﻿using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;

namespace PDFTools
{
    public partial class PdfMergeView : UserControl, IPdfTool
    {
        public override string Text
        {
            get { return "合并PDF"; }
        }

        public PdfMergeView()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            using (MemoryStream stream = new MemoryStream(Properties.Resources.pdf))
            {
                Image image = Image.FromStream(stream);
                imageListPdf.Images.Add(image);
                listViewFiles.LargeImageList = imageListPdf;
                listViewFiles.SmallImageList = imageListPdf;
            }
        }

        public void SaveProperties()
        {
        }


        /// <summary>
        /// 合并PDF文件
        /// </summary>
        public void Exec()
        {
            if (listViewFiles.Items.Count <= 0)
            {
                MessageBox.Show("没添加需要合并的PDF文件", "合并PDF", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(textBoxOutPDF.Text))
            {
                MessageBox.Show("没设置输出文件", "合并PDF", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string outputFileName = textBoxOutPDF.Text;
            using (PdfDocument mergedDocument = new PdfDocument())
            {
                foreach (ListViewItem item in listViewFiles.Items)
                {
                    string pdfFile = item.Text;
                    PdfDocument inputDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
                    foreach (PdfPage page in inputDocument.Pages)
                    {
                        mergedDocument.AddPage(page);
                    }
                    inputDocument.Close();
                }
                mergedDocument.Save(outputFileName);
            }
            MessageBox.Show("PDF文件合并完成，并已保存到:\n" + outputFileName, "合并PDF", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "PDF Files (*.pdf)|*.pdf|All Files (*.*)|*.*";
            dialog.Title = "选择PDF文件";
            dialog.Multiselect = true;
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            foreach (string fileName in dialog.FileNames)
            {
                //if (listViewFiles.Items.Find(fileName, false) == null)
                {
                    listViewFiles.Items.Add(new ListViewItem(fileName, 0));
                }
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewFiles.SelectedItems)
            {
                listViewFiles.Items.Remove(item);
            }
        }

        private void buttonPdfFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "PDF Files (*.pdf)|*.pdf";
            dialog.Title = "选择输出PDF文件";
            dialog.OverwritePrompt = true;
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            textBoxOutPDF.Text = dialog.FileName;
        }

        private void listBoxPdfFile_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            foreach (ListViewItem item in listViewFiles.SelectedItems)
            {
                listViewFiles.Items.Remove(item);
            }

        }

    }
}
